//
//  ByAddressAddNewVC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/23.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByAddressAddNewVC.h"

// cell
#import "ByCellAddressAddNewNormal.h"

@interface ByAddressAddNewVC () <UITableViewDelegate, UITableViewDataSource, UITextViewDelegate>

@property (nonatomic, strong) UITableView *table;

@end

@implementation ByAddressAddNewVC
{
    // cell的一些视图和属性
    UITextField *_userTf;
    UITextField *_mobileTf;
    NSString    *_area;
    NSString    *_street;
    
    UITextView  *_detailAddressTv;
    UILabel     *_detailAddressPlaceholderLabel;
    
    UIButton    *_toggleButton;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"新建收货地址";
    
    [self addLeftBackBarButtonItem];
    
    [self createTableView];
}

#pragma mark - 创建视图
- (void)createTableView {
    
    UIButton *commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    commitButton.backgroundColor = BY_THEME_COLOR;
    [commitButton setTitle:@"提交" forState:UIControlStateNormal];
    [commitButton setTitleColor:BY_GOLD_COLOR forState:UIControlStateNormal];
    [commitButton addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitButton];
    [commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@44);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        } else {
            make.left.right.bottom.equalTo(self.view);
            
        }
    }];
    
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.bottom.equalTo(commitButton.mas_top);
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 选择头像
    if (indexPath.section == 0) {
        
        // 收货人
        if (indexPath.row == 0) {
            ByCellAddressAddNewNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"userCell"];
            if (!cell) {
                cell = [[ByCellAddressAddNewNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"userCell"];
                cell.title = @"收  货  人：";
                cell.subTitle = nil;
                _userTf = [UITextField new];
                _userTf.textColor = [UIColor blackColor];
                _userTf.textAlignment = NSTextAlignmentRight;
                [cell.contentView addSubview:_userTf];
                [_userTf mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(cell);
                    make.left.equalTo(cell.titleLabel.mas_right).offset(10);
                    make.right.equalTo(cell).offset(-10);
                    make.centerY.equalTo(cell);
                }];
            }
            
            return cell;
        }
        // 联系号码
        else if (indexPath.row == 1) {
            
            ByCellAddressAddNewNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"mobileCell"];
            if (!cell) {
                cell = [[ByCellAddressAddNewNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mobileCell"];
                cell.title = @"联系电话：";
                cell.subTitle = nil;
                _mobileTf = [UITextField new];
                _mobileTf.textColor = [UIColor blackColor];
                _mobileTf.textAlignment = NSTextAlignmentRight;
                [cell.contentView addSubview:_mobileTf];
                [_mobileTf mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(cell);
                    make.left.equalTo(cell.titleLabel.mas_right).offset(10);
                    make.right.equalTo(cell).offset(-10);
                    make.centerY.equalTo(cell);
                }];
            }
            
            return cell;
        }
        // 所在地区
        else if (indexPath.row == 2) {
            
            ByCellAddressAddNewNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"areaCell"];
            if (!cell) {
                cell = [[ByCellAddressAddNewNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"areaCell"];
                cell.title = @"所在地区：";
            }
            cell.subTitle = _area == nil ? @"请选择" : _area;
            
            return cell;
        }
        // 街区
        else if (indexPath.row == 3) {
            
            ByCellAddressAddNewNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"streetCell"];
            if (!cell) {
                cell = [[ByCellAddressAddNewNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"streetCell"];
                cell.title = @"街        区：";
            }
            cell.subTitle = _street == nil ? @"请选择" : _street;
            
            return cell;
        }
        // 详细地址
        else if (indexPath.row == 4) {
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailCell"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detailCell"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                _detailAddressTv = [UITextView new];
                _detailAddressTv.textColor = [UIColor blackColor];
                _detailAddressTv.delegate = self;
                _detailAddressTv.font = FONT(14);
                [cell.contentView addSubview:_detailAddressTv];
                [_detailAddressTv mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.edges.equalTo(cell.contentView).insets(UIEdgeInsetsMake(5, 5, 5, 5));
                }];
                
                _detailAddressPlaceholderLabel = [[UILabel alloc] init];
                _detailAddressPlaceholderLabel.text = @"请输入地址详细信息：如哪个小区几栋几号";
                _detailAddressPlaceholderLabel.font = FONT(14);
                _detailAddressPlaceholderLabel.textAlignment = NSTextAlignmentLeft;
                _detailAddressPlaceholderLabel.textColor = [UIColor lightGrayColor];
                [_detailAddressTv addSubview:_detailAddressPlaceholderLabel];
                [_detailAddressPlaceholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.top.right.equalTo(_detailAddressTv).offset(5);
                    make.height.equalTo(@20);
                }];
            }
            
            
            return cell;
        }
    }
    // 默认地址
    else if (indexPath.section == 1) {
        
        ByCellAddressAddNewNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"defaultCell"];
        if (!cell) {
            cell = [[ByCellAddressAddNewNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultCell"];
            cell.title = @"设为默认地址";
            cell.subTitle = nil;
            
            _toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_toggleButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
            [_toggleButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
            [cell.contentView addSubview:_toggleButton];
            [_toggleButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(15, 15));
                make.right.equalTo(cell.contentView).offset(-10);
                make.centerY.equalTo(cell.contentView);
            }];
        }

        
        return cell;
    }

    
    return [UITableViewCell new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.section == 0) {
        if (indexPath.row == 4) {
            
            return 80;
        }
    }
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    
    // 选择头像
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [_userTf becomeFirstResponder];
        }
        if (indexPath.row == 1) {
            [_mobileTf becomeFirstResponder];
        }
        if (indexPath.row == 2) {
            [self selectArea];
        }
        if (indexPath.row == 4) {
            [_detailAddressTv becomeFirstResponder];
        }
    }
    else if (indexPath.section == 1) {
        _toggleButton.selected = !_toggleButton.selected;
    }
}


#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    _detailAddressPlaceholderLabel.hidden = true;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    
    _detailAddressPlaceholderLabel.hidden = textView.text.length > 0;
}

#pragma mark - 点击事件
// 选择地区
- (void)selectArea {
    
    ByViewCitySelect *view = [[ByViewCitySelect alloc] init];
    [view setDidSelectCity:^(ByModelCity *model) {
        
        _area = model.fullAddress;
        [self.table reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }];
    [view show];
}
// 提交
- (void)commit {
    
//    NSString *userName = _userTf.text;
//    NSString *mobile = _mobileTf.text;
//    NSString *detailAddress = _detailAddressTv.text;
//    BOOL isDefatil = _toggleButton.selected;
    
}

@end
